xquery version "3.0";

module namespace goods = "shop/goods";

declare namespace api = "api";

import module namespace config="http://exist-db.org/xquery/apps/config" at "../config.xqm";

declare %private function goods:storage(){
    collection($config:app-root || "/data/all/goods/")
};

declare %private function goods:renrer-one($good){
    <ul class="thumbnails masonry">
        <li class="span3 masonry-brick">
            <div class="thumbnail">
                <img alt="" src="good-image/{$good/@id/string()}"/>
                <div class="caption">
                    <h5>{$good/title/text()}</h5>
                    <p>{$good/comment/text()}</p>
                    <p>
                        <a class="btn primary details" href="#" rel="ajax/1.html">
                            <i class="icon-zoom-in"/>
                        </a>
                        <a class="btn addto" href="#" rel="1">Add to <i class="icon-shopping-cart"/></a>
                    </p>
                    <p><span class="label label-info price">{$good/parameters/price/string()} руб.</span></p>
                </div>
            </div>
        </li>
    </ul>
};

declare %private function local:filter($item, $filter){
    if (not($filter/category) and not($filter/model)) then true()
    else 
        $item/parameters/category eq $filter/category or $item/parameters/model eq $filter/model
};

declare
    %api:goods-list
function goods:list($data){
    <show-goods-list>{
        for $good in goods:storage()/tovar[local:filter(., $data)]
        return goods:renrer-one($good)
    }</show-goods-list>
};

(: cellpadding="0" cellspacing="0" border="0" class="display" id="goodsTable":)

declare
    %api:goods-list-in-table
function goods:list-in-table($data){
    <update-goods-table>
        <table class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>Number</th>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Model</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th>Number</th>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Model</th>
                    <th>Price</th>
                </tr>
            </tfoot>
            <tbody>{
                for $good in goods:storage()/tovar[local:filter(., $data)]
                return
                    <tr>
                        <td>{$good/system/number/text()}</td>
                        <td>{$good/parameters/title/text()}</td>
                        <td>{$good/parameters/category/text()}</td>
                        <td>{$good/parameters/model/text()}</td>
                        <td>{$good/parameters/price/text()}</td>
                    </tr>
            }</tbody>
        </table>
    </update-goods-table>
};

declare
    %api:models-list
function goods:models($data){
    <set-models>{
        for $model in distinct-values(goods:storage()//model)
        return <model>{$model}</model>
    }</set-models>
};

declare
    %api:models-list-html
function goods:models-html($data){
    <show-html-models>{
        for $model in distinct-values(goods:storage()/tovar[local:filter(., $data)]//model)
        return 
            <ul class="thumbnails masonry">
                <li class="span3 masonry-brick">
                    <div class="thumbnail">
                        <img alt="" src="model-image/{$model}"/>
                        <div class="caption" value="{$model}">
                            <h5>{$model}</h5>
                        </div>
                    </div>
                </li>
            </ul>
    }</show-html-models>
};

declare
    %api:categories-list
function goods:categories($data){
    <set-categories>{
        for $cat in distinct-values(goods:storage()//category)
        return <category>{$cat}</category>
    }</set-categories>
};

declare
    %api:get-good
function goods:get($data){
    ()
};

declare
    %api:create-good
function goods:create($data){
    let $login := xmldb:login("/db/", "admin", "")
    let $data := 
        <tovar id="{util:uuid()}">
            <parameters>
                <category>{$data/category/text()}</category>
                <model>{$data/car-model/text()}</model>
                <price>{$data/price/text()}</price>
                <title>{$data/title/text()}</title>
                <image>{util:binary-to-string(request:get-uploaded-file-data("image"))}</image>
            </parameters>
            <system>
                <number>{$data/cat-number/text()}</number>
                <created></created>
            </system>
            <description>{util:parse-html($data/description)/*:HTML/*:BODY/*}</description>
        </tovar>
    let $store := xmldb:store($config:app-root || "/data/all/goods/", $data/@id || ".xml", $data)
    return 
        <success>
            <id>{$data/@id/string()}</id>
        </success>
};

declare
    %api:delete-good
function goods:delete($data){
    if ($data/id) then
        <success>{
            update delete goods:storage()/tovar[@id/string() eq $data/id/string()]
        }</success>
    else
        <error>id not defined</error>
};

declare
    %api:store-good
function goods:store($data){
    ()
};